﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI
{
    public delegate void OkHander();
    public delegate void OkHander0(bool isArress);
    public class UIFieldAskPk : MonoBehaviour
    {
        public UIFieldRunMap page;
        public MovePlayer movePlayer;
        public OkHander0 ok;
        public UILabel redTip;

        public GameObject normalBtn;
        public GameObject redBtn;
        
        
        string tipStr = "";

        void AddHeroIcon()
        {
            var template = AssetLoad.ResourceManager.Instance.LoadResource("prefab/HeroIcon");
            var heroInst = GameObject.Instantiate(template) as GameObject;
            heroInst.SetActive(true);
            heroInst.transform.SetParent(this.heroObj.transform);
            heroInst.transform.localScale = Vector3.one;
            heroInst.transform.localPosition = Vector3.zero;
            heroInst.transform.SetParent(this.heroObj.transform.parent);

            this.heroIcon = heroInst.GetComponent<HeroIcon>();
            this.heroObj = heroInst;
        }

        public void Init(MovePlayer movePlayer, OkHander0 ok, string tipStr)
        {
            this.movePlayer = movePlayer;
            this.ok = ok;

            var kills = movePlayer.info.playerSide.kills;
            var needSend = false;
            var killerColor = Dict.Blo.DictKillerBlo.GetKillerData(kills);

            if (killerColor.jailFlag == 1)
            {
                needSend = true;
            }

            

            SetButton(needSend);

            this.tipStr = tipStr;

            SetHero();
            Show();
            
        }

        
        void SetButton(bool needSend)
        {
            redBtn.SetActive(false);
            normalBtn.SetActive(false);

            if (needSend)
            {
                redTip.text = TableTextBlo.GetText("kill_red_player_tip");
                redBtn.SetActive(true);

            }
            else
            {
                normalBtn.SetActive(true);
                redTip.text = "";
            }
        }

        /// <summary>
        /// 掠夺
        /// </summary>
        public void OnClickOk()
        {
            if (string.IsNullOrEmpty(this.tipStr))
            {
                ok(false);
            }
            else
            {
                BoxManager.CreatePopupTextBox(this.tipStr);
            }
            Hide();
        }
        

        /// <summary>
        /// 遣送
        /// </summary>
        public void OnClickOk0()
        {
            if (string.IsNullOrEmpty(this.tipStr))
            {
                ok(true);
            }
            else
            {
                BoxManager.CreatePopupTextBox(this.tipStr);
            }
            Hide();
        }

        public void OnCreateTeam()
        {
            var room = Room.RoomManager.currentRoom as Room.RoomField;
            if (room != null && room.netManager!=null)
            {
                var net = room.netManager as Room.RoomNetField;
                if(net!=null)
                {
                    net.SendCSTeamCreateAndInvite(this.movePlayer.info.playerId, this.movePlayer.info.serverId);
                }
            }

            BoxManager.CreatePopupTextBox(TableTextBlo.GetText("field_team_send_group"));
            Hide();
        }
        public void OnClickCancel()
        {
            Hide();
        }

        void Show()
        {
            gameObject.SetActive(true);
        }

        void Hide()
        {
            if(needUnregister)
            {
                NetAPI.UnregisterSocketHandler(OpDefine.SCGetHp, SCGetHp);
            }
            gameObject.SetActive(false);
        }

        public GameObject heroObj;
        [HideInInspector]
        public HeroIcon heroIcon;
        public UILabel heroGroup;
        public UILabel heroPower;
        public UILabel heroLine;
        public UILabel heroLv;
        public UILabel heroName;
        public UISprite heroHp;
        void SetHero()
        {
            if(heroIcon == null)
                AddHeroIcon();
            heroIcon.SetHeroWithId(movePlayer.info.warId);
            
            heroName.text = movePlayer.info.playerName;
            heroLine.text = TableTextBlo.GetText("current_server") + movePlayer.info.serverId;
            heroLv.text = TableTextBlo.GetText("level_label") + movePlayer.info.level+""; 
            heroPower.text = movePlayer.info.power+"";
            heroGroup.text = movePlayer.info.unionName;
            heroHp.fillAmount = Mathf.Clamp01((float)movePlayer.info.playerSide.curHp);

            needUnregister = true;
            NetAPI.RegistSocketHandler(OpDefine.SCGetHp, SCGetHp);
            var get = new CSGetHp();
            get.playerId = movePlayer.info.playerId;
            NetAPI.SendSocket(OpDefine.CSGetHp, get);
        }

        bool needUnregister = false;

        void SCGetHp(object obj)
        {
            needUnregister = false;
            var hp = obj as SCGetHp;
            heroHp.fillAmount = Mathf.Clamp01((float)hp.hp);
            movePlayer.info.playerSide.curHp = hp.hp;
            NetAPI.UnregisterSocketHandler(OpDefine.SCGetHp,SCGetHp);
        }


    }
}